package com.example.bausip;

import java.util.ArrayList;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private EditText username;
	private EditText password;
	private TextView forgotpassword, loginTxt;
	boolean isChecked = false;
	private CheckBox keeploggedin;
	private Button submit;
	String userId ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActionBar().hide();
		setContentView(R.layout.activity_login);

		username = (EditText) findViewById(R.id.user_name);
		password = (EditText) findViewById(R.id.password);
		forgotpassword = (TextView) findViewById(R.id.forgotpassword);
		loginTxt = (TextView) findViewById(R.id.login);
		keeploggedin = (CheckBox) findViewById(R.id.keeploggedin);
		submit = (Button) findViewById(R.id.submit);
		SharedPreferences settings1 = getSharedPreferences("PREFS_NAME", 0);
		isChecked = settings1.getBoolean("isChecked", false);
		String lang = settings1.getString("lang", null);
		User user = new User(username.getText().toString(), null);
		user.setEncPassword(password.getText().toString());
		
	}
	public void login(View v)
	{
		userId = username.getText().toString();
		sendLogin(username.getText().toString(),password.getText().toString());
	}
	public void sendLogin(String user, String pass)
	{
		String serverURL = new myURL("login.php?login="+user+"&pass="+pass,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "getLoggedIn", true);
		p.get();
	}

	public void getLoggedIn(String s, String error) {
		if (error == null) {
			User u = new APIManager().getLoggedIn(s);
			int id = u.getId();
			if(id==0)
			{
				Toast.makeText(getApplicationContext(), R.string.wrongcredentials,
						Toast.LENGTH_SHORT).show();
				return;
			}
			Log.d("ray","seting"+id);
			sip.setUserId(id);
			Intent i = null;
			if (userId.startsWith("E")) {
				// go to inst page
				sip.setInstructor(true);
				i = new Intent(this, InstructorHomeActivity.class);
			} else if (userId.startsWith("A")) {
				sip.setIsAdmin(true);
				i = new Intent(this, AdminHomeActivity.class);
			} else //if (Converter.toInt(userId) > 0)
			{
				sip.setStudent(true);
				i = new Intent(this, StudentHomeActivity.class);
			}
			i.putExtra("notify"	, u.isNotification());
			
			startActivity(i);
		} else {
			Toast.makeText(getApplicationContext(), R.string.wrongcredentials,
					Toast.LENGTH_SHORT).show();
		}
	}

	public void forgotpassword(View view) {
		// Intent i = new Intent(LoginActivity.this,
		// ForgotPasswordActivity.class);
		// startActivity(i);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(R.string.exit)
				.setMessage(R.string.exitquest)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							@SuppressLint("NewApi")
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								LoginActivity.this.finishAffinity();
							}
						}).setNegativeButton("No", null).show();
	}

}
